--#include "data\config\activityconfig\DailyActivity\DarkBoxConfig.lua" once   --暗之宝箱

DarkBox_MonsterListIndex = 0
DarkBox_MonsterIndex = 1
--注册一些事件
function DarkBoxResisterProc()
	SceneReliveDispatcher.register(DarkBoxConfig.sceneId, OnDarkBoxRelive)
	SceneLogoutDispatcher.register( DarkBoxConfig.sceneId, OnDarkBoxExitScene )	
	SceneExitDispatcher.register(DarkBoxConfig.sceneId,  OnDarkBoxExitScene)
	ItemAddDispatcher.register( DarkBoxConfig.itemId, DarkBoxItemChange )             --注册获得某个道具触发的事件
    ItemRemoveDispatcher.register( DarkBoxConfig.itemId, DarkBoxItemChange )
    for i,v in ipairs(DarkBoxConfig.Monsters) do
    	for i2,v2 in ipairs(v.Monster) do
    		local monsterEntityType = System.getMonsterEntityTypeById(v2.monsterId)
    		if monsterEntityType == enGatherMonster then
	    		GatherMonsterDispatcher.register(v2.monsterId, DarkBoxCanGather)
	    	end
    	end
    end
end
--活动开启
function DarkBoxStart()
	DarkBoxClear()
	local dVar = System.getDyanmicVar()
	dVar.DarkBoxBeginTime  = System.getCurrMiniTime()			--活动开启时间
	for i,v in ipairs(DarkBoxConfig.Monsters) do
		Actor.regScriptTimer(System.getGlobalNpc(), 0, 0, v.refreshTime*1000,-1, "DarkBoxRefreshMonster",i)
	end
end
--活动数据清理
function DarkBoxClear()
	local dVar = System.getDyanmicVar()
	dVar.DarkBoxBeginTime  = nil
	dVar.DarkBoxMonsterData = nil
	Actor.unregScriptCallback(sysarg, "DarkBoxRefreshMonster")
	ClearDarkBoxAllMonsters()
end
--活动结束
function DarkBoxEnd()
	DarkBoxClear()
	DarkBoxKickPlayer()
end
--踢出玩家
function DarkBoxKickPlayer()
    local playerList = LuaHelp.getSceneActorListById(DarkBoxConfig.sceneId) or {}
	for k,v in pairs(playerList)do  --踢出场景
		Actor.returnCity(v)
	end 
end
--退出场景
function OnDarkBoxExitScene(sysarg)
	CloseActivityRightPanel(sysarg)
	SetPropertyHeadItemValue(sysarg, 1, 0)
	local itemCount = Actor.getItemCount(sysarg, DarkBoxConfig.itemId)
	Actor.removeItem(sysarg, DarkBoxConfig.itemId, itemCount,-1,-1,-1, Lang.LogTips.DarkBoxConsume,LogIds.DarkBoxConsumeLogId)
end
--安全复活
function OnDarkBoxRelive(sysarg)
	local reliveIndex = math.random(#DarkBoxConfig.RelivePos)
	local reliveCfg =  DarkBoxConfig.RelivePos[reliveIndex]
	Actor.enterScene(sysarg,DarkBoxConfig.sceneId, unpack(reliveCfg))
end
--是否活动开启
function IsDarkBoxStart()
	local dVar = System.getDyanmicVar()
	if dVar.DarkBoxBeginTime then
		local nCurTime = System.getCurrMiniTime()
		if nCurTime - dVar.DarkBoxBeginTime <= DarkBoxConfig.activityTime then
			return true
		end
	end
end
--是否在场景内
function IsInDarkBoxScene( sceneId )
	if sceneId == DarkBoxConfig.sceneId then
		return true
	end
	return false
end
--进入活动
function DarkBoxEnter(sysarg)
	--等级检测
	if(Actor.checkActorLevel(sysarg, DarkBoxConfig.level[2], DarkBoxConfig.level[1])~=true)then
		return Actor.sendTipmsg( sysarg, string.format(Lang.ScriptTips.ActivityGuide002,DarkBoxConfig.level[2]), ttFlyTip )
	end
	local sysVar = System.getDyanmicVar()
	if not IsDarkBoxStart() then	
		return Actor.sendTipmsg( sysarg, Lang.ScriptTips.ActivityGuide003, ttFlyTip )
	end	
	if not Actor.enterScene(sysarg,unpack(DarkBoxConfig.EnterPos)) then
		return
	end
	InitDarkBoxRightPanel(sysarg)

	
	DarkBoxSetHeadItem(sysarg)
end

--刷怪
function DarkBoxRefreshMonster(NpcPtr, monsterListIndex)
	monsterListIndex = tonumber(monsterListIndex)
	local dVar = System.getDyanmicVar()
	if not dVar.DarkBoxMonsterData then
		dVar.DarkBoxMonsterData = {}
	end
	if not dVar.DarkBoxMonsterData[monsterListIndex] then
		dVar.DarkBoxMonsterData[monsterListIndex] = {}
	end
	local isRefresh = false
	local monsterCfg = DarkBoxConfig.Monsters[monsterListIndex]
	local hScene = Fuben.getSceneHandleById(DarkBoxConfig.sceneId, 0)
	for i,v in ipairs(monsterCfg.Monster) do
		if v.range then
			local nHadMonsterCount = Fuben.getMyMonsterCount(hScene, v.monsterId)
			local nRefreshCount = v.count - nHadMonsterCount
			if nRefreshCount > 0 then
				isRefresh = true
				Fuben.createMonstersInRange(hScene,v.monsterId, v.range[1],v.range[2],v.range[3],v.range[4],nRefreshCount,v.livetime)
			end
		elseif v.posX and v.posY then
			local nHadMonsterCount = dVar.DarkBoxMonsterData[monsterListIndex][i] or 0
			if nHadMonsterCount == 0 then
				isRefresh = true
				local pMonster = Fuben.createMonster(hScene,v.monsterId,v.posX,v.posY,v.livetime)
				dVar.DarkBoxMonsterData[monsterListIndex][i] = 1
				Actor.setMonsterValue( pMonster, DarkBox_MonsterListIndex, monsterListIndex)
				Actor.setMonsterValue( pMonster, DarkBox_MonsterIndex, i)
			end
		end
	end
	if isRefresh then
		if monsterCfg.broadMsg then
			Fuben.sendMsgToScene(hScene, monsterCfg.broadMsg, ttScreenCenter + ttChatWindow)
		end
		local playerList = LuaHelp.getSceneActorListById(DarkBoxConfig.sceneId) or {}	
		for k,v in pairs(playerList)do
			ChangeDarkBoxRightPanel(v)
		end
	end
end
--怪物死亡
function OnDarkBoxMonDie(monster, sysarg, monId)
	local sceneId = Actor.getSceneId(sysarg)
    if not IsInDarkBoxScene(sceneId) or not IsDarkBoxStart()  then		
        return 
    end
    local monsterListIndex = Actor.getMonsterValue(monster, DarkBox_MonsterListIndex)
    local monsterIndex = Actor.getMonsterValue(monster, DarkBox_MonsterIndex)
    if not DarkBoxConfig.Monsters[monsterListIndex] then
    	return
    end
    local oneMoneCfg =  DarkBoxConfig.Monsters[monsterListIndex].Monster[monsterIndex]
    if not oneMoneCfg then
    	return
    end
    local dVar = System.getDyanmicVar()
	if not dVar.DarkBoxMonsterData then
		dVar.DarkBoxMonsterData = {}
	end
	if not dVar.DarkBoxMonsterData[monsterListIndex] then
		dVar.DarkBoxMonsterData[monsterListIndex] = {}
	end
  	dVar.DarkBoxMonsterData[monsterListIndex][monsterIndex] = nil

  	--扣除物品
  	if oneMoneCfg.needItemCount then
	  	local consume = {{type = 0, id = DarkBoxConfig.itemId, count = oneMoneCfg.needItemCount}}
	 	 --	if  CheckConsumeCond(sysarg, consume) then
		DoConsumeCond(sysarg, consume, 1, LogIds.DarkBoxConsumeLogId, Lang.LogTips.DarkBoxConsume, false )
		--end
	end

  	if monsterListIndex == #DarkBoxConfig.Monsters then
  		--大宝箱刷新面板
  		ChangeDarkBoxRightPanel(sysarg)
  	end
end
--清除怪物
function ClearDarkBoxAllMonsters()
	local hScene = Fuben.getSceneHandleById(DarkBoxConfig.sceneId, 0)
	for i, v in ipairs( DarkBoxConfig.Monsters ) do
		for i2,v2 in ipairs(v.Monster) do
			Fuben.clearMonster(hScene,v2.monsterId)
		end
	end
end
--是否能采集
function DarkBoxCanGather(sysarg, pMonster, nMonsterId)
	local sceneId = Actor.getSceneId(sysarg)
    if not IsInDarkBoxScene(sceneId) or not IsDarkBoxStart()  then		
        return false
    end
	local monsterListIndex = Actor.getMonsterValue(pMonster, DarkBox_MonsterListIndex)
    local monsterIndex = Actor.getMonsterValue(pMonster, DarkBox_MonsterIndex)
    if not DarkBoxConfig.Monsters[monsterListIndex] then
    	return false
    end
    local oneMoneCfg =  DarkBoxConfig.Monsters[monsterListIndex].Monster[monsterIndex]
    if not oneMoneCfg then
    	return false
    end
    if  oneMoneCfg.needItemCount then
    	local itemCount = Actor.getItemCount(sysarg, DarkBoxConfig.itemId)
    	if itemCount < oneMoneCfg.needItemCount then
    		Actor.sendTipmsg(sysarg, Lang.ScriptTips.DarkBox004, ttFlyTip)
    		return false
    	end
    end
    if oneMoneCfg.needBagCount then
    	local emptypGrid = Item.getBagEmptyGridCount(sysarg)
    	if emptypGrid < oneMoneCfg.needBagCount then
    		Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.COM0012,oneMoneCfg.needBagCount), ttFlyTip)
    		return false
    	end
    end
	return true
end
--获取活动剩余时间
function GetDarkBoxLeftTime()
	local dVar = System.getDyanmicVar()
	if not dVar.DarkBoxBeginTime then
		return 0
	end

	local now = System.getCurrMiniTime()	
	local leftTime = dVar.DarkBoxBeginTime + DarkBoxConfig.activityTime - now
	if leftTime > 0 then
		return leftTime
	end
	return 0
end
--获取怪物波数
function GetDarkBoxRefreshWave(startTime, nowTime, refreshTime)
	local wave = math.floor((nowTime - startTime)/refreshTime)+1
	return wave
end
--获取变化的面板内容
function GetDarkBoxPanelcontents(sysarg)
	local contents = {}
	local now = System.getCurrMiniTime()
	local dVar = System.getDyanmicVar()
	local hScene = Fuben.getSceneHandleById(DarkBoxConfig.sceneId, 0)
	--当前怪物波数：%d
	--local contentStr = GetDarkBoxRefreshWave(dVar.DarkBoxBeginTime, now, DarkBoxConfig.Monsters[1].refreshTime)
	--contents[1]	= string.format(Lang.ScriptTips.panelCont25,contentStr)
	--超级大宝箱，是否已刷新
	local nBigBoxMonsterId = DarkBoxConfig.Monsters[#DarkBoxConfig.Monsters].Monster[1].monsterId
	local pBigBoxMonster = Fuben.getMonsterPtrById(hScene, nBigBoxMonsterId)
	local sBigBoxMonsterName = System.getMonsterNameById(nBigBoxMonsterId)
	if pBigBoxMonster and not Actor.isDeath(pBigBoxMonster) then
		contentStr = string.format(Lang.ScriptTips.panelCont42, sBigBoxMonsterName, Lang.ScriptTips.panelCont43)
	else
		contentStr = string.format(Lang.ScriptTips.panelCont42, sBigBoxMonsterName, Lang.ScriptTips.panelCont45)
	end
	contents[1]	= contentStr
	
	--物品数量
	local itemCount = Actor.getItemCount(sysarg, DarkBoxConfig.itemId)
    contents[2]	= string.format(Lang.ScriptTips.panelCont49, Item.getItemName(DarkBoxConfig.itemId),itemCount,DarkBoxConfig.itemMaxCount)
    return contents
end
--初始化右侧面板
function InitDarkBoxRightPanel(sysarg)
	local sceneId = Actor.getSceneId(sysarg)
	if not IsInDarkBoxScene(sceneId) or not IsDarkBoxStart()  then		
		return
	end

	local activityType 		= 2 			--活动
	
	local panelInfo 		= {}
	panelInfo.sts 			= 1  				--进行中

	panelInfo.title 		= Actor.getSceneName(sysarg,0,0)
	panelInfo.timeTitle 	= Lang.ScriptTips.panelCont01			--剩余时间
	panelInfo.restTime		= GetDarkBoxLeftTime()

	panelInfo.contentTitle 	= Lang.ScriptTips.panelCont30  			
	panelInfo.contents 		= GetDarkBoxPanelcontents(sysarg)

	panelInfo.awardTitle	= ""			--没有此title
	panelInfo.awards 		= {}			--没有可显示的奖励

	panelInfo.buttons = {1}	--1：退出副本，2-单倍领奖，3-双倍领奖，4-查看排行榜，5-押镖寻路, 6-打开答题面板，7-放弃押镖，8-寻找活动NPC

	OpenActivityRightPanel( sysarg, activityType, enRightPanelActivity_DarkBox, panelInfo )
end
--物品数量变化
function DarkBoxItemChange(sysarg,itemId,itemCount)
	local sceneId = Actor.getSceneId( sysarg )
	if not IsInDarkBoxScene(sceneId) or not IsDarkBoxStart()  then		
		return
	end
	DarkBoxSetHeadItem(sysarg)
	ChangeDarkBoxRightPanel(sysarg)
end
--头顶图标
function DarkBoxSetHeadItem(sysarg)
	local itemCount = Actor.getItemCount(sysarg, DarkBoxConfig.itemId)
	if itemCount > 0 then
		SetPropertyHeadItemValue(sysarg, 1, DarkBoxConfig.itemId)  --增加头部item
	else
		SetPropertyHeadItemValue(sysarg, 1, 0)  --增加头部item
	end
end
--改变面板
function ChangeDarkBoxRightPanel(sysarg)
	local sceneId = Actor.getSceneId( sysarg )
	if not IsInDarkBoxScene(sceneId) or not IsDarkBoxStart()  then		
		return
	end

	local panelInfo 		= {}
	panelInfo.sts 			= 1  				--1-进行中,2-已结束
	panelInfo.contents 		= GetDarkBoxPanelcontents(sysarg)

	panelInfo.buttons = {1}	--1：退出副本，2-单倍领奖，3-双倍领奖，4-查看排行榜，5-押镖寻路, 6-打开答题面板，7-放弃押镖，8-寻找活动NPC
	
	ChangeActivityRightPanel(sysarg, panelInfo)
end

DarkBoxResisterProc()



